<template>
  <div class="update-birthday">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  components: {},
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onConfirm () {
      this.$toast.loading({
        message: '保存中',
        forbidclick: true
      })
      const time = `${this.currentDate.getFullYear()}-${this.currentDate.getMonth() +
        1}-${this.currentDate.getDate()}`

      await this.$http.put('api/user/logoInfo', {
        data: {
          birthday: time,
          username: this.user.username
        }
      }).then(({ data: res }) => {
        if (res.code === 0) {
          const data = {
            ...this.user,
            birthday: time
          }
          this.$emit('input', time)
          this.$emit('close')
          this.$store.commit('setUser', data)
          this.$toast.success('保存成功')
        } else {
          this.$toast.fail('修改失败')
        }
      })
    }
  }
}
</script>

<style scoped lang="less"></style>
